package com.example.restful05.controller;

import com.example.restful05.service.StudentService;
import org.apache.catalina.User;
import com.example.restful05.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    StudentService StudentService;

    /**
     * 查询所有学生
     * @return
     */
    @GetMapping("/AllStudents")
    public List<Student> getAllStudents(){
        return StudentService.list();
    }

    /**
     * 根据 id 查询指定学生
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){
        Student student = (Student) StudentService.getById(id);
        return student;
    }

    /**
     * 插入学生
     * @param student
     * @return
     */
    @PostMapping("/student")
    public String addStudent(@RequestBody Student student){
        if (StudentService.save(Student)){
            return "插入学生成功！";
        }
        else return "插入学生失败";
    }

    /**
     * 修改学生信息
     * @param student
     * @return
     */
    @PutMapping("/student")
    public String updateStudent(@RequestBody Student student){
        if (StudentService.updateById(Student)){
            return "更新学生成功！";
        }
        else return "更新学生失败";
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    @DeleteMapping("/student")
    public String deleteStudentById(Integer id){
        if (StudentService.removeById(id)){
            return "删除学生成功！";
        }
        else return "删除学生失败！";
    }
}

